﻿//========================================================================//
//      Copyright 2012, Smilodon Studio Inc.
//      All rights reserved.
//========================================================================//
/**
 *	base
 *      @author: CYS
 */


var canvas;
var g2D;

var height;
var width;
var fullWindow = true;
var ratio = 1;

var time = 0;
var dtime = 0;
var startime;
var lasttime;
var g_infoShow = new infoShow();
var bClear = false;
var clearTime = 0;
var bMouseDown = false;

var lineColor = 'rgba(255, 255, 225, 1)';
var maxNumPmusic = 350;
var numpMusices = 0;
var pMusices = [];

function canvasResize() {
	ratio = window.innerWidth / canvas.width;
	canvas.width = window.innerWidth;
	canvas.height = window.innerHeight;
	width = canvas.width;
	height = canvas.height;
	if (!g2D) g2D = canvas.getContext("2d");
	g2D.lineWidth = 2;
	g2D.lineCap = 'round';
	g2D.strokeStyle = lineColor;
	g2D.fillStyle = lineColor;

}

function init() {
	canvas = document.getElementById('canvas2D');
	canvasResize();
	//	var clearButton = document.getElementById("clearButton");
	//	clearButton.textContent = ' C ';
	//	clearButton.addEventListener( 'click', function ( event )
	//	{
	//		bClear = true;
	//		clearTime = 50;
	//	}, false);
	var touchable = 'createTouch' in document;
	if (touchable) {
		canvas.addEventListener('touchstart', onTouchStart, false);
		canvas.addEventListener('touchmove', onTouchMove, false);
		canvas.addEventListener('touchend', onTouchEnd, false);
	} else {
		canvas.addEventListener('mousedown', onTouchStart, false);
		canvas.addEventListener('mousemove', onTouchMove, false);
		canvas.addEventListener('mouseup', onTouchEnd, false);
	}

	canvas.addEventListener('contextmenu', function(event) {
		event.preventDefault();
	}, false);

	//	var PmusicImg = new Image();
	//	PmusicImg.src = "Pmusic.png";
	//	PmusicImg.src = "gravel.jpg";
	for (var i = 0; i < maxNumPmusic; i++) {
		var b = new pmusic();
		pMusices.push(b);
	}
}

var bTimeout = false;
window.onresize = function() {
	if (fullWindow) canvasResize();
	bTimeout = true;
}

var first = true;
var lastOrien;

window.ondeviceorientation = function(e) {
	if (first) {
		lastOrien = window.orientation;
		first = false;
	}
	if (window.orientation !== lastOrien) {
		window.onresize();
		lastOrien = window.orientation;
	}

	g_infoShow.add("alpa: ", e.alpha);
	g_infoShow.add("beta: ", e.beta);
	g_infoShow.add("gamma: ", e.gamma);
	g_infoShow.add("orientation: ", window.orientation);
}

window.onblur = function() {
	bTimeout = true;
}

function drawScene() {
	// draw background color 
	g2D.clearRect(0, 0, canvas.width, canvas.height);
	//	if(bClear && clearTime > 0)
	//	{
	//		g2D.save();
	//		g2D.globalAlpha = 0.1;
	//		g2D.fillStyle = '#fff';
	//		g2D.fillRect(0, 0, canvas.width,canvas.height);
	//		g2D.restore();
	//		clearTime--;
	//		if(clearTime == 0)bClear = false;
	//	}
	g2D.save();
	//	g2D.fillStyle = lineColor;
	//	g2D.shadowOffsetX = 0;
	//	g2D.shadowOffsetY = 0;
	//	g2D.shadowBlur    = 8;
	//	g2D.shadowColor   = lineColor;
	var j = numpMusices;
	for (var i = 0; i < maxNumPmusic; i++, j++) {
		if (j == maxNumPmusic) j = 0;
		if (pMusices[j].life > 0) pMusices[j].draw();
	}

	g2D.restore();

	g_infoShow.show();

}



function onTouchStart(event) {
	// Prevent the browser from doing its default thing (scroll, zoom)
	event.preventDefault();

	g_infoShow.add("canvasLeft:   ", canvas.clientLeft);
	g_infoShow.add("canvasRight:   ", canvas.clientTop);

	if (event.clientX) // on mouse down
	{
		var c = new Color(rnd(0.7) + 0.3, rnd(0.7) + 0.3, rnd(0.7) + 0.3);
		pMusices[numpMusices].begin(event.clientX, event.clientY, c, 0);
		numpMusices++;
		if (maxNumPmusic == numpMusices) numpMusices = 0;
		bMouseDown = true;
	}
//pad
	if (!event.touches) return;
	// on touch star
	for (var i = 0; i < event.touches.length; i++) {
		var bInLast = false;
		for (var j = 0; j < numpMusices; j++) {
			if (event.touches[i].identifier === pMusices[j].id) bInLast = true;
		}
		if (!bInLast) {
			var c = new Color(rnd(0.7) + 0.3, rnd(0.7) + 0.3, rnd(0.7) + 0.3);

			pMusices[numpMusices].begin(event.touches[i].clientX, event.touches[i].clientY, c, event.touches[i].identifier);
			numpMusices++;
			if (maxNumPmusic == numpMusices) numpMusices = 0;

		}
	}

}

function onTouchMove(event) {
	event.preventDefault();

	if (event.clientX && bMouseDown) // on mouse move
	{
		var c = new Color(rnd(0.7) + 0.3, rnd(0.7) + 0.3, rnd(0.7) + 0.3);
		pMusices[numpMusices].begin(event.clientX, event.clientY, c, 0);
		numpMusices++;
		if (maxNumPmusic == numpMusices) numpMusices = 0;
	}
//pad
	if (!event.touches) return;

	for (var i = 0; i < event.touches.length; i++) {
		for (var j = 0; j < numpMusices; j++) {
			if (event.touches[i].identifier === pMusices[j].id) {
				pMusices[j].add(event.touches[i].clientX, event.touches[i].clientY);
			}
		}
	}


}

function onTouchEnd(event) {
	event.preventDefault();

	if (event.clientX) // on mouse up
	{
		bMouseDown = false;
	}
	if (!event.touches) return;

	for (var i = 0; i < event.touches.length; i++) {
		for (var j = i; j < numpMusices; j++) {
			if (event.touches[i].identifier === pMusices[j].id) {
				var tempC = pMusices[j].c;
				var tempSize = pMusices[j].size;
				pMusices[numpMusices].x = pMusices[i].x;
				pMusices[numpMusices].y = pMusices[i].y;
				pMusices[numpMusices].c = pMusices[i].c;
				pMusices[numpMusices].id = pMusices[i].identifier;
				pMusices[i].x = pMusices[i].lastx = event.touches[i].clientX;
				pMusices[i].y = pMusices[i].lasty = event.touches[i].clientY;
				pMusices[i].c = tempC;
				pMusices[i].c = tempSize;
				pMusices[i].id = event.touches[i].identifier;
				if (i === 0) numpMusices += 1;
			}
		}

	}
	numpMusices = event.touches.length;

}

function gameloop() {
	window.requestAnimFrame(gameloop);
	if (bTimeout) {
		lasttime = Date.now();
		bTimeout = false;
	}
	dtime = Date.now() - lasttime;
	time = Date.now() - startime;
	drawScene();
	lasttime = Date.now();

}


function game() {
	init();
	gameloop();
}